package com.gilgame.bukkit;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
 *
 * @author Gilgame
 * @date 27 November 2011
 */
public class CommandHandler
{
    public boolean run(CommandSender sender, Command command, String label, String[] args)
    {
        if (!hasPermission(sender, command))
        {
            sender.sendMessage("You do not have permission to access this command.");
            return true;
        }

        if (command.getName().equalsIgnoreCase("clearentity"))
        {
            return new CommandClearEntity().run(sender, args);
        }

        return true;
    }

    private boolean hasPermission(CommandSender sender, Command command)
    {
        if (!(sender instanceof Player))
        {
            return true;
        }

        if (sender.isOp())
        {
            return true;
        }

        if (sender.hasPermission("clearentity.*"))
        {
            return true;
        }

        if (sender.hasPermission("clearentity." + command.getName().toLowerCase()))
        {
            return true;
        }

        return false;
    }
}
